package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.PermissionDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.pojo.Permission;
import com.itheima.health.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName CheckItemServiceImpl
 * @Description TODO
 * @Author ly
 * @Company 深圳黑马程序员
 * @Date 2019/9/11 9:55
 * @Version V1.0
 */
@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionDao permissionDao;

    @Override
    public PageResult pageQuery(Integer currentPage, String queryString, Integer pageSize) {

        //使用分页插件PageHelper, 设置当前页, 每页最多显示的记录数
        PageHelper.startPage(currentPage, pageSize);

        //响应分页插件的page对象
        Page<Permission> page = permissionDao.selectByCondition(queryString);

        return new PageResult(page.getTotal(), page.getResult());

    }

    @Override
    public void add(Permission permission, List<Integer> roleIds) {
        permissionDao.add(permission);
    }

    @Override
    public void update(Permission permission) {
        permissionDao.update(permission);
    }

    @Override
    public Permission findById(Integer id) {
        Permission permission = permissionDao.findById(id);

        return permission;
    }

    @Override
    public List<Permission> selectAll() {
        List<Permission> list = permissionDao.selectAll();

        return list;
    }

    @Override
    public void delete(Integer id) {
        try {
            permissionDao.deleteById(id);
        } catch (Exception e) {
            throw new RuntimeException("删除的检查项在检查组中存在引用关系，不能删除，请先删除中间表的数据");
        }
    }

}
